package com.daluzy.chmanager.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daluzy.chmanager.MyApplication;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.adapter.AreaAdapter;
import com.daluzy.chmanager.bean.Area;
import com.daluzy.chmanager.bean.Employee;
import com.daluzy.chmanager.bean.Manager;
import com.daluzy.chmanager.bean.TestPoint;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class ManagerFragment extends Fragment {
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private Context context;
    private List<Area> areaList = new ArrayList<>();
    private LoadingDialog loadingDialog;
    private Manager manager;
    private Employee employee;
    private int type;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_manager_home_daluzy, container, false);
        ButterKnife.bind(this, view);
        rvData.setLayoutManager(new LinearLayoutManager(getContext()));
        loadingDialog = new LoadingDialog(getContext());

        srlRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                type = MyApplication.getIntance().getUserType();
                if (type == 2){
                    employee = MyApplication.getIntance().getEmployee();
                }else {
                    manager = MyApplication.getIntance().getManager();
                }
                getArea();
            }
        });
        srlRefresh.setEnableLoadMore(false);
        return view;
    }

    public void getArea() {
        if (loadingDialog != null)
            loadingDialog.show();
        BmobQuery<Area> query = new BmobQuery<>();
        if (type == 2){
            query.addWhereEqualTo("area_name",employee.getEmployee_area());
        }else {
            query.addWhereEqualTo("area_school",manager.getSchool_name());
        }

        query.order("-createdAt")
                .findObjects(new FindListener<Area>() {
                    @Override
                    public void done(List<Area> object, BmobException e) {
                        srlRefresh.finishRefresh();
                        if (e == null) {
                            if (object != null && object.size() > 0) {
                                getTestPoint(object);
                            }else {
                                if (loadingDialog != null)
                                    loadingDialog.close();
                            }
                        }else {
                            if (loadingDialog != null)
                                loadingDialog.close();
                        }

                    }
                });
    }


    public void getTestPoint(List<Area> areas) {
        if (areaList != null)
            areaList.clear();
        for (Area area : areas) {
            BmobQuery<TestPoint> query = new BmobQuery<>();
            query.addWhereEqualTo("area",area.getArea_id());
            query.findObjects(new FindListener<TestPoint>() {
                        @Override
                        public void done(List<TestPoint> object, BmobException e) {
                            if (e == null) {
                                if (object != null && object.size() > 0) {
                                    area.setTestPointList(object);
                                }
                            }
                            areaList.add(area);
                            if (areaList.size() == areas.size()) {
                                if (loadingDialog != null)
                                    loadingDialog.close();
                                AreaAdapter areaAdapter = new AreaAdapter(context, areaList);
                                rvData.setAdapter(areaAdapter);
                            }
                            if (loadingDialog != null)
                                loadingDialog.close();
                        }
                    });
        }


    }

    @Override
    public void onResume() {
        super.onResume();
        type = MyApplication.getIntance().getUserType();
        if (type == 2){
            employee = MyApplication.getIntance().getEmployee();
        }else {
            manager = MyApplication.getIntance().getManager();
        }
        getArea();
    }
}
